//
//  LSZVistorView.swift
//  醒浪微博
//
//  Created by 雷顺哲 on 16/7/9.
//  Copyright © 2016年 LSZ. All rights reserved.
//

import UIKit
import SnapKit

class LSZVistorView: UIView {
    
    override init(frame: CGRect){
        super.init(frame: frame)
        setUpUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    定义登录或者注册执行的闭包
    var loginClosure: (() -> ())?

    // MARK:懒加载控件
//    旋转的视图
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
//    罩层
    private lazy var maskIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
//    icon
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
//    消息内容
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
//        多行显示
        label.numberOfLines = 0
//        居中显示
        label.textAlignment = .Center
        
        return label
    }()
    
//    注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
//        添加点击事件
        button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
//        设置字体的大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
//        设置文本及文本颜色
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)        
        return button
    }()
    //    登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        //        添加点击事件
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        //        设置字体的大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //        设置文本及文本颜色
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    // MARK:添加控件
    private func setUpUI()
    {
//        设置背景颜色
        backgroundColor = UIColor(white: 237 / 255 , alpha: 1)
        
//        添加控件
        addSubview(cycleImageView)
        addSubview(maskIconImageView)
        addSubview(iconImageView)
        addSubview(registerButton)
        addSubview(loginButton)
        addSubview(messageLabel)
        
        
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskIconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
    }
    
    // MARK:开启旋转动画
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
//        动画的轨迹
        animation.toValue = 2 * M_PI
//        动画时长
        animation.duration = 20
//        动画重复次数
        animation.repeatCount = MAXFLOAT
//        失去焦点不释放资源
        animation.removedOnCompletion = false

        cycleImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    // MARK:外部修改访客视图信息
    func updataVisitorInfo(message message: String?, imageName: String?) {
        if let msg = message, img = imageName {
            messageLabel.text = msg
            iconImageView.image = UIImage(named: img)
            cycleImageView.hidden = true
        } else {
            cycleImageView.hidden = false
            startAnimation()
        }
    }
    
    
    @objc private func registerAction() {
        print("注册点击")
        loginClosure?()
    }
    @objc private func loginAction() {
        print("登录点击")
        loginClosure?()
    }
}
